package com.wuhan.template.module.find.activity;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseActivity;
import com.wuhan.template.constant.Urls;
import com.wuhan.template.module.find.adapter.MeishiListAdapter;
import com.wuhan.template.module.find.adapter.MeishiPagerAdapter;
import com.wuhan.template.module.find.bean.MeishiBean;
import com.wuhan.template.module.home.listener.MyViewPageListener;
import com.wuhan.template.utils.EventMessage;
import com.wuhan.template.utils.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class MeishiActivity extends BaseActivity {

    @BindView(R.id.ms_list)
    PullToRefreshListView listView;


    private List<String> picUrls = new ArrayList<>();
    private MeishiPagerAdapter pagerAdapter;
    private ViewPager mVp;
    private List<MeishiBean.ListBean> mList = new ArrayList<>();
    private Handler handler = new Handler();
    private MeishiListAdapter listAdapter;
    private TextView mVpTitle;
    private RadioGroup mRg;
    private int i = 0;
    private List<String> contentUrls = new ArrayList<>();


    @Override
    protected int getLayoutId() {
        return R.layout.activity_meishi_layout;
    }

    @Override
    protected void initViews() {


        listAdapter = new MeishiListAdapter(this, mList);
        pagerAdapter = new MeishiPagerAdapter(picUrls, contentUrls, this);
        listView.setAdapter(listAdapter);

        AbsListView.LayoutParams layoutParams =
                new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
        View header = getLayoutInflater().inflate(R.layout.meishi_viewpager_layout, listView, false);
        header.setLayoutParams(layoutParams);

        mVp = (ViewPager) header.findViewById(R.id.vp_meishi);
        mVpTitle = (TextView) header.findViewById(R.id.tv_meishi);
        mRg = (RadioGroup) header.findViewById(R.id.ms_rg);
        ListView lv = listView.getRefreshableView();
        lv.addHeaderView(header);
        mVp.setAdapter(pagerAdapter);


    }

    private void addTextAndPoints(final List<String> vpTitles) {
        int pointNum = vpTitles.size();
        for (int i = 0; i < pointNum; i++) {
            RadioButton tempButton = new RadioButton(this);
            tempButton.setPadding(40, 0, 0, 0);
            tempButton.setButtonDrawable(R.drawable.point_radion_selector);           // 设置按钮的样式
            tempButton.setId(i);

            mRg.addView(tempButton);
        }
        mVpTitle.setText(vpTitles.get(0));
        mRg.check(0);
        mVp.addOnPageChangeListener(new MyViewPageListener() {

            @Override
            public void onPageSelected(int position) {
                mVpTitle.setText(vpTitles.get(position));
                mRg.check(position);

            }
        });


    }

    @OnClick(R.id.ms_back)
    public void back(View view) {
        finish();
    }


    @Override
    protected void loadData() {
        Intent intent = getIntent();
        String title = (String) intent.getCharSequenceExtra("title");
        if ("亲子".equals(title)) {
            OkHttpUtils.get(Urls.FIND_QINZI, null);
        } else if ("美食".equals(title)) {
            OkHttpUtils.get(Urls.FIND_MEISHI, null);
        }

    }

    @Override
    public void onGetResult(EventMessage message) {
        super.onGetResult(message);
        if (message.type.equals(Urls.FIND_MEISHI) || message.type.equals(Urls.FIND_QINZI)) {
            Gson gson = new Gson();
            MeishiBean meishi = gson.fromJson(message.result, MeishiBean.class);
            List<String> vpTitles = new ArrayList<>();

            for (MeishiBean.SlideBean slide : meishi.getSlide()) {
                picUrls.add(slide.getIndexpic().getHost()
                        + slide.getIndexpic().getDir()
                        + slide.getIndexpic().getFilepath()
                        + slide.getIndexpic().getFilename());
                contentUrls.add(slide.getContent_url());
                vpTitles.add(slide.getTitle());
            }

            mList.addAll(meishi.getList());
            listAdapter.notifyDataSetChanged();
            pagerAdapter.notifyDataSetChanged();
            addTextAndPoints(vpTitles);

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mVp.setCurrentItem(i++);
                    handler.postDelayed(this, 2500);
                }
            }, 2500);
        }

    }
}
